﻿using System;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Piser.Common
{
    [TemplateVisualStateAttribute(Name = "Pressed", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "MouseOver", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Normal", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Disabled", GroupName = "CommonStates")]
    [TemplateVisualStateAttribute(Name = "Unfocused", GroupName = "FocusStates")]
    [TemplateVisualStateAttribute(Name = "Focused", GroupName = "FocusStates")]
    public class ContentElement : Control, Piser.Core.IControl
    {
        public ContentElement()
        {
            this.MouseEnter += UIElement_MouseEnter;
            this.MouseLeave += UIElement_MouseLeave;
            this.MouseLeftButtonDown += UIElement_MouseLeftButtonDown;
            this.MouseLeftButtonUp += UIElement_MouseLeftButtonUp;
            this.GotFocus += UIElement_GotFocus;
            this.LostFocus += UIElement_LostFocus;
        }

        private void UIElement_LostFocus(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "Unfocused", true);
        }

        private void UIElement_GotFocus(object sender, RoutedEventArgs e)
        {
            VisualStateManager.GoToState(this, "Focused", true);
        }

        private void UIElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
        }

        private void UIElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "Pressed", true);
        }

        private void UIElement_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
        }

        private void UIElement_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "MouseOver", true);
        }


        #region Property
        /// <summary>
        /// 正文
        /// </summary>
        /// <returns>正文</returns>
        public string Content
        {
            get { return GetContent(this); }
            set { SetContent(this, value); }
        }
        public Type ContentType { get;set; }
        public double MaxLendth { get; set; }
        public double MinLength { get; set; }
        public string Regular { get; set; }
        public bool Required { get; set; }
        public string LengthMessage { get; set; }
        public string RegularMessage { get; set; }
        public string RequiredMessage { get; set; }
        public string ContentTypeMessage { get; set; }
        DataTemplate ContentTemplate { get; set; }

        public new bool IsEnabled
        {
            get
            {
                return GetIsEnabled(this);
            }
            set
            {
                SetIsEnabled(this, value);
                if (value)
                {
                    VisualStateManager.GoToState(this, "Normal", true);
                }
                else
                {
                    VisualStateManager.GoToState(this, "Disabled", true);
                }
            }
        }

        public string Message
        {
            get
            {
                return GetMessage(this);
            }
        }
        #endregion

        #region DependencyProperty
        private static DependencyProperty ContentProperty =
            DependencyProperty.RegisterAttached("Content", typeof(String), typeof(ContentElement), new PropertyMetadata(String.Empty));

        private static DependencyProperty ContentTemplateProperty =
            DependencyProperty.RegisterAttached("ContentTemplate", typeof(DataTemplate), typeof(ContentElement), new PropertyMetadata(new DataTemplate()));

        private new static DependencyProperty IsEnabledProperty =
            DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(ContentElement), new PropertyMetadata(true));

        private static DependencyProperty MessageProperty =
            DependencyProperty.RegisterAttached("Message", typeof(String), typeof(ContentElement), new PropertyMetadata(String.Empty));
        #endregion

        #region Private Method
        private static void SetContent(DependencyObject element,String value)
        {
            element.SetValue(ContentProperty, value);
        }
        private static String GetContent(DependencyObject element)
        {
            return (String)element.GetValue(ContentProperty);
        }

        private static void SetContentTemplate(DependencyObject element,DataTemplate value)
        {
            element.SetValue(ContentTemplateProperty, value);
        }

        private static DataTemplate GetContentTemplate(DependencyObject element)
        {
            return (DataTemplate)element.GetValue(ContentTemplateProperty);
        }

        private static void SetIsEnabled(DependencyObject element,bool value)
        {
            element.SetValue(IsEnabledProperty, value);
        }

        private static bool GetIsEnabled(DependencyObject element)
        {
            return (bool)element.GetValue(IsEnabledProperty);
        }

        private static void SetMessage(DependencyObject element,String Value)
        {
            element.SetValue(MessageProperty, Value);
        }

        private static String GetMessage(DependencyObject element)
        {
            return (String)element.GetValue(MessageProperty);
        }
        #endregion
        public bool ValidContent()
        {
            if (this.MaxWidth > 0 && this.Content.Length > this.MaxWidth)
            {
                ShowMessage(LengthMessage);
                return false;
            }
            else if (this.MinLength > 0 && this.Content.Length < this.MinLength)
            {
                ShowMessage(LengthMessage);
                return false;
            }
            else if (this.Required && String.IsNullOrEmpty(this.Content))
            {
                ShowMessage(RequiredMessage);
                return false;
            }
            else if (!String.IsNullOrEmpty(this.Regular))
            {
                try
                {
                    Regex regular = new Regex(this.Regular);
                    if (!regular.IsMatch(this.Content))
                        ShowMessage(RegularMessage);
                    return false;
                }
                catch
                {
                    ShowMessage("Regular Pattern Is Not Vaild.");
                    return false;
                }
            }
            else
            {
                return true;
            }
        }

        public void ShowMessage(string ErrorMessage)
        {
            SetMessage(this, ErrorMessage);
        }
    }
}
